 package com.example.demofdfs.util;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import org.apache.commons.lang3.SerializationUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtil {

	public static void main(String[] args) throws UnsupportedEncodingException {
		String key = "cps_task_table";
//		Set<byte[]> object = hKey(key);
//		for (byte[] bs : object) {
//			Object obj = SerializationUtils.deserialize(bs);
//			System.out.println(obj);
//		}
//		
//		List<byte[]> list = hValue(key);
//		for (byte[] bs : list) {
//			Object obj = SerializationUtils.deserialize(bs);
//			System.out.println(obj);
//		}
		
	}

//	private static Jedis jedis = new Jedis();
//	
//	public static Set<byte[]> hKey(String key) {
//		
//		return jedis.hkeys(key.getBytes());
//	}
//	public static List<byte[]> hValue(String key) {
//		
//		return jedis.hvals(key.getBytes());
//	}
//	
	private static JedisPool pool;
	private static String host = "localhost";
	
	private static void init() {
		JedisPoolConfig config =  new JedisPoolConfig();
		pool = new JedisPool(config, host);
	}
	
	private static Jedis connect() {
		if (pool == null)
			init();
		return pool.getResource();
	}
	
	public static Object execute(CallBack<? super Jedis> c) {
		Object obj = null;
		Jedis j = connect();
		try {
			obj = c.accept(j);
		} finally {
			j.close();
		}
		return obj;
	}
	
	public static interface CallBack<T> {
		Object accept(T t);
	}
	
}
